#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>
#include <pthread.h>

/*
 * 一次唤醒一个线程
 */

using namespace std;

int tickets = 1000;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void* start_routine(void* args)
{
    string name = static_cast<const char*>(args);
    while(true)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond,&mutex);
        //判断省略
        cout<<name<<" -> "<<tickets<<endl;
        tickets--;
        pthread_mutex_unlock(&mutex);
    }
}

int main()
{
    pthread_t t1,t2;
    pthread_create(&t1,nullptr,start_routine,(void*)"thread 1");
    pthread_create(&t1,nullptr,start_routine,(void*)"thread 2");

    while(true)
    {
        sleep(1);
        pthread_cond_signal(&cond);
        cout<<"main thread wakeup one thread..."<<endl;
    }
    pthread_join(t1,nullptr);
    pthread_join(t2,nullptr);

    return 0;
}

#if 0
main thread wakeup one thread...thread 1 -> 
1000
main thread wakeup one thread...
thread 2 -> 999
main thread wakeup one thread...
thread 1 -> 998
main thread wakeup one thread...
thread 2 -> 997
main thread wakeup one thread...
thread 1 -> 996
main thread wakeup one thread...
thread 2 -> 995
main thread wakeup one thread...
thread 1 -> 994
main thread wakeup one thread...
thread 2 -> 993
#endif
